#include "stdafx.h"
#include "../sgp/ImageData16bpp.h"

namespace ja2{ namespace sgp
{
	ImageData16bpp::ImageData16bpp(DataType_t &&pData, std::uint_least32_t Size)
		: ImageDataRGB(std::move(pData), Size)	
	{
	}

	void ImageData16bpp::do_blit(char *pBuffer, char *pSource, std::uint_least16_t Width, std::uint_least16_t Row) const
	{
		// Compute the actual row
		char * const p_src_row = pSource + Width * Row * 2;

		for(std::uint_least16_t i = 0; i < Width; ++i)
		{
			// Pointer to current destination pixel
			char * const p_pixel_dest = pBuffer + i * 4;
			// Pointer to current source pixel
			char * const p_pixel_src = p_src_row + i * 2;
			// Put the 565 RGB 16-bit value into a 32-bit RGB value
			char red = (static_cast<unsigned char>(p_pixel_src[1]) & 0xF8) >> 3;
			char green = (static_cast<unsigned char>(p_pixel_src[0]) >> 5) | ((static_cast<unsigned char>(p_pixel_src[1]) & 0x07) << 3);
			char blue = static_cast<unsigned char>(p_pixel_src[0]) & 0x1F;
			p_pixel_dest[0] = (blue * int(255)) / 31;
			p_pixel_dest[1] = (green * int(255)) / 63;
			p_pixel_dest[2] = (red * int(255)) / 31;
		}
	}
} /*sgp*/} /*ja2*/